<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html id="htmlId">
<head>
    <title>Coverage Report :: Inky</title>
    <style type="text/css">
        @import "../../.css/coverage.css";
    </style>
</head>

<body>
<div class="header"></div>

<div class="content">
    <div class="breadCrumbs">
        [ <a href="../../index.html">all classes</a> ]
        [ <a href="../index.html">nl.tudelft.jpacman.npc.ghost</a> ]
    </div>

    <h1>Coverage Summary for Class: Inky (nl.tudelft.jpacman.npc.ghost)</h1>

    <table class="coverageStats">
        <tr>
            <th class="name">Class</th>
            <th class="coverageStat
">
                Class, %
            </th>
            <th class="coverageStat
">
                Method, %
            </th>
            <th class="coverageStat
">
                Line, %
            </th>
        </tr>
        <tr>
            <td class="name">Inky</td>
            <td class="coverageStat">
  <span class="percent">
    0%
  </span>
                <span class="absValue">
    (0/ 1)
  </span>
            </td>
            <td class="coverageStat">
  <span class="percent">
    0%
  </span>
                <span class="absValue">
    (0/ 4)
  </span>
            </td>
            <td class="coverageStat">
  <span class="percent">
    0%
  </span>
                <span class="absValue">
    (0/ 22)
  </span>
            </td>
        </tr>

    </table>

    <br/>
    <br/>


    <div class="sourceCode"><i>1</i>&nbsp;package nl.tudelft.jpacman.npc.ghost;
        <i>2</i>&nbsp;
        <i>3</i>&nbsp;import nl.tudelft.jpacman.board.Direction;
        <i>4</i>&nbsp;import nl.tudelft.jpacman.board.Square;
        <i>5</i>&nbsp;import nl.tudelft.jpacman.board.Unit;
        <i>6</i>&nbsp;import nl.tudelft.jpacman.level.Player;
        <i>7</i>&nbsp;import nl.tudelft.jpacman.npc.Ghost;
        <i>8</i>&nbsp;import nl.tudelft.jpacman.sprite.Sprite;
        <i>9</i>&nbsp;
        <i>10</i>&nbsp;import java.util.List;
        <i>11</i>&nbsp;import java.util.Map;
        <i>12</i>&nbsp;import java.util.Optional;
        <i>13</i>&nbsp;
        <i>14</i>&nbsp;/**
        <i>15</i>&nbsp; * &lt;p&gt;
        <i>16</i>&nbsp; * An implementation of the classic Pac-Man ghost Inky.
        <i>17</i>&nbsp; * &lt;/p&gt;
        <i>18</i>&nbsp; * &lt;b&gt;AI:&lt;/b&gt; Inky has the most complicated AI of all. Inky considers two things:
        Blinky&#39;s
        <i>19</i>&nbsp; * location, and the location two grid spaces ahead of Pac-Man. Inky draws a
        <i>20</i>&nbsp; * line from Blinky to the spot that is two squares in front of Pac-Man and
        <i>21</i>&nbsp; * extends that line twice as far. Therefore, if Inky is alongside Blinky
        <i>22</i>&nbsp; * when they are behind Pac-Man, Inky will usually follow Blinky the whole
        <i>23</i>&nbsp; * time. But if Inky is in front of Pac-Man when Blinky is far behind him,
        <i>24</i>&nbsp; * Inky tends to want to move away from Pac-Man (in reality, to a point very
        <i>25</i>&nbsp; * far ahead of Pac-Man). Inky is affected by a similar targeting bug that
        <i>26</i>&nbsp; * affects Speedy. When Pac-Man is moving or facing up, the spot Inky uses to
        <i>27</i>&nbsp; * draw the line is two squares above and left of Pac-Man.
        <i>28</i>&nbsp; * &lt;p&gt;
        <i>29</i>&nbsp; * Source: http://strategywiki.org/wiki/Pac-Man/Getting_Started
        <i>30</i>&nbsp; * &lt;/p&gt;
        <i>31</i>&nbsp; *
        <i>32</i>&nbsp; * @author Jeroen Roosen
        <i>33</i>&nbsp; */
        <b class="nc"><i>34</i>&nbsp;public class Inky extends Ghost {</b>
        <i>35</i>&nbsp;
        <i>36</i>&nbsp; private static final int SQUARES_AHEAD = 2;
        <i>37</i>&nbsp;
        <i>38</i>&nbsp; /**
        <i>39</i>&nbsp; * The variation in intervals, this makes the ghosts look more dynamic and
        <i>40</i>&nbsp; * less predictable.
        <i>41</i>&nbsp; */
        <i>42</i>&nbsp; private static final int INTERVAL_VARIATION = 50;
        <i>43</i>&nbsp;
        <i>44</i>&nbsp; /**
        <i>45</i>&nbsp; * The base movement interval.
        <i>46</i>&nbsp; */
        <i>47</i>&nbsp; private static final int MOVE_INTERVAL = 250;
        <i>48</i>&nbsp;
        <i>49</i>&nbsp; /**
        <i>50</i>&nbsp; * Creates a new &quot;Inky&quot;.
        <i>51</i>&nbsp; *
        <i>52</i>&nbsp; * @param spriteMap The sprites for this ghost.
        <i>53</i>&nbsp; */
        <i>54</i>&nbsp; public Inky(Map&lt;Direction, Sprite&gt; spriteMap) {
        <b class="nc"><i>55</i>&nbsp; super(spriteMap, MOVE_INTERVAL, INTERVAL_VARIATION);</b>
        <i>56</i>&nbsp; }
        <i>57</i>&nbsp;
        <i>58</i>&nbsp; /**
        <i>59</i>&nbsp; * {@inheritDoc}
        <i>60</i>&nbsp; *
        <i>61</i>&nbsp; * &lt;p&gt;
        <i>62</i>&nbsp; * Inky has the most complicated AI of all. Inky considers two things: Blinky&#39;s
        <i>63</i>&nbsp; * location, and the location two grid spaces ahead of Pac-Man. Inky
        <i>64</i>&nbsp; * draws a line from Blinky to the spot that is two squares in front of
        <i>65</i>&nbsp; * Pac-Man and extends that line twice as far. Therefore, if Inky is
        <i>66</i>&nbsp; * alongside Blinky when they are behind Pac-Man, Inky will usually
        <i>67</i>&nbsp; * follow Blinky the whole time. But if Inky is in front of Pac-Man when
        <i>68</i>&nbsp; * Blinky is far behind him, Inky tends to want to move away from Pac-Man
        <i>69</i>&nbsp; * (in reality, to a point very far ahead of Pac-Man). Inky is affected
        <i>70</i>&nbsp; * by a similar targeting bug that affects Speedy. When Pac-Man is moving or
        <i>71</i>&nbsp; * facing up, the spot Inky uses to draw the line is two squares above
        <i>72</i>&nbsp; * and left of Pac-Man.
        <i>73</i>&nbsp; * &lt;/p&gt;
        <i>74</i>&nbsp; *
        <i>75</i>&nbsp; * &lt;p&gt;
        <i>76</i>&nbsp; * &lt;b&gt;Implementation:&lt;/b&gt;
        <i>77</i>&nbsp; * To actually implement this in jpacman we have the following approximation:
        <i>78</i>&nbsp; * first determine the square of Blinky (A) and the square 2
        <i>79</i>&nbsp; * squares away from Pac-Man (B). Then determine the shortest path from A to
        <i>80</i>&nbsp; * B regardless of terrain and walk that same path from B. This is the
        <i>81</i>&nbsp; * destination.
        <i>82</i>&nbsp; * &lt;/p&gt;
        <i>83</i>&nbsp; */
        <i>84</i>&nbsp; @Override
        <i>85</i>&nbsp; public Optional&lt;Direction&gt; nextAiMove() {
        <b class="nc"><i>86</i>&nbsp; assert hasSquare();</b>
        <b class="nc"><i>87</i>&nbsp; Unit blinky = Navigation.findNearest(Blinky.class, getSquare());</b>
        <b class="nc"><i>88</i>&nbsp; Unit player = Navigation.findNearest(Player.class, getSquare());</b>
        <i>89</i>&nbsp;
        <b class="nc"><i>90</i>&nbsp; if (blinky == null || player == null) {</b>
        <b class="nc"><i>91</i>&nbsp; return Optional.empty();</b>
        <i>92</i>&nbsp; }
        <i>93</i>&nbsp;
        <b class="nc"><i>94</i>&nbsp; assert player.hasSquare();</b>
        <b class="nc"><i>95</i>&nbsp; Square playerDestination = player.squaresAheadOf(SQUARES_AHEAD);</b>
        <i>96</i>&nbsp;
        <b class="nc"><i>97</i>&nbsp; List&lt;Direction&gt; firstHalf = Navigation.shortestPath(blinky.getSquare(),</b>
        <i>98</i>&nbsp; playerDestination, null);
        <i>99</i>&nbsp;
        <b class="nc"><i>100</i>&nbsp; if (firstHalf == null) {</b>
        <b class="nc"><i>101</i>&nbsp; return Optional.empty();</b>
        <i>102</i>&nbsp; }
        <i>103</i>&nbsp;
        <b class="nc"><i>104</i>&nbsp; Square destination = followPath(firstHalf, playerDestination);</b>
        <b class="nc"><i>105</i>&nbsp; List&lt;Direction&gt; path = Navigation.shortestPath(getSquare(),</b>
        <i>106</i>&nbsp; destination, this);
        <i>107</i>&nbsp;
        <b class="nc"><i>108</i>&nbsp; if (path != null &amp;&amp; !path.isEmpty()) {</b>
        <b class="nc"><i>109</i>&nbsp; return Optional.ofNullable(path.get(0));</b>
        <i>110</i>&nbsp; }
        <b class="nc"><i>111</i>&nbsp; return Optional.empty();</b>
        <i>112</i>&nbsp; }
        <i>113</i>&nbsp;
        <i>114</i>&nbsp;
        <i>115</i>&nbsp; private Square followPath(List&lt;Direction&gt; directions, Square start) {
        <b class="nc"><i>116</i>&nbsp; Square destination = start;</b>
        <i>117</i>&nbsp;
        <b class="nc"><i>118</i>&nbsp; for (Direction d : directions) {</b>
        <b class="nc"><i>119</i>&nbsp; destination = destination.getSquareAt(d);</b>
        <b class="nc"><i>120</i>&nbsp; }</b>
        <i>121</i>&nbsp;
        <b class="nc"><i>122</i>&nbsp; return destination;</b>
        <i>123</i>&nbsp; }
        <i>124</i>&nbsp;}
    </div>
</div>

<div class="footer">

    <div style="float:right;">generated on 2023-04-25 16:45</div>
</div>
</body>
</html>
